package com.woniu.his.pms.app.appImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.his.pms.app.util.JwtUtil;
import com.woniu.his.pms.client.PmsPatientClient;
import com.woniu.his.pms.client.param.AddPatientClientParam;
import com.woniu.his.pms.client.param.PhoneLoginParam;
import com.woniu.his.pms.client.result.PatientLoginResult;
import com.woniu.his.pms.client.result.PatientResult;
import com.woniu.his.pms.client.result.StartDiagnosePatientResult;
import com.woniu.his.pms.infra.model.Patient;
import com.woniu.his.pms.service.PatientService;
import com.woniu.his.pms.service.dto.PatientDto;
import com.woniu.his.pms.service.dto.PatientLoginDto;
import com.woniu.his.pms.service.dto.StartDiagnosePatientDto;
import com.woniu.his.pms.service.param.AddPatientServiceParam;
import com.woniu.his.pms.service.param.PhoneLoginServiceParam;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: ZJH
 * @description:
 * @date: 2022-11-25 23:37
 */
@RestController
@RequestMapping("pms")
@Slf4j
public class PatientClientImpl implements PmsPatientClient {

    @Resource
    private PatientService patientService;

    @Override
    public Result<PatientResult> addPatient(AddPatientClientParam param) {
        log.info("新增患者参数：{}",param);
        AddPatientServiceParam patientServiceParam = BeanUtil.copyProperties(param, AddPatientServiceParam.class);
        PatientDto patientDto = patientService.addPatient(patientServiceParam);
        return Result.getInstance(PatientResult.class).setData(BeanUtil.copyProperties(patientDto,PatientResult.class));
    }

    @Override
    public Result<List<PatientResult>> queryPatient(List<Long> patientIds) {
        log.info("患者ids：{}",patientIds);
        List<PatientDto> patientDtos = patientService.queryPatient(patientIds);
        return Result.getInstanceList(PatientResult.class).setData(BeanUtil.copyToList(patientDtos,PatientResult.class));
    }

    @Override
    public Result<PatientResult> queryPatientByIdentity(String identity) {
        log.info("患者身份证：{}",identity);
        PatientDto patientDto = patientService.queryPatientByIdentity(identity);
        return Result.getInstance(PatientResult.class).setData(BeanUtil.copyProperties(patientDto,PatientResult.class));
    }

    @Override
    public Result<StartDiagnosePatientResult> queryPatientById(Long patientId) {
        log.info("患者id：{}",patientId);
        StartDiagnosePatientDto startDiagnosePatientDto = patientService.queryPatientById(patientId);
        StartDiagnosePatientResult startDiagnosePatientResult = BeanUtil.copyProperties(startDiagnosePatientDto, StartDiagnosePatientResult.class);
        return Result.getInstance(StartDiagnosePatientResult.class).setData(startDiagnosePatientResult);
    }

    @Override
    public Result<PatientLoginResult> patientPhoneLogin(PhoneLoginParam param) {

        log.info("用户登录传回的信息：{}",param);
        PhoneLoginServiceParam phoneLoginServiceParam=BeanUtil.copyProperties(param,PhoneLoginServiceParam.class);
        PatientLoginDto patient=patientService.patientPhoneLogin(phoneLoginServiceParam);
        PatientLoginResult result=BeanUtil.copyProperties(patient,PatientLoginResult.class);
        HashMap<String, Object> map = new HashMap<>();
        map.put("patientId",patient.getId());
        String token = JwtUtil.create(map);
        result.setToken(token);
        return Result.getInstance(PatientLoginResult.class).setData(result);
    }

    @Override
    public Result checkPatientIsExist(Long patientId) {

        log.info("传回来的病人ID:{}",patientId);
        Patient one = patientService.getById(patientId);
        if (one==null) return Result.getInstance().setCode(500).setMsg("病人不存在");
        return Result.getInstance();
    }
}
